<?php

namespace App\Http\Middleware;
use App\Dao\UsersDao;
use App\Services\Cache;
use App\Services\Code;
use Closure;
use Illuminate\Http\Request;
class UserMiddleware
{


    /**
     * Handle an incoming request.
     *
     * @param  Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $datas = $request->all();


        foreach ($datas as $value){
            $value = strtolower($value);
            if(strpos($value,'script') != false){
                echo json_encode(Code::message('非法数据',301),JSON_UNESCAPED_UNICODE);exit;//没有传用户的Header
            }
        }



        if(request()->header('uid') == ''){
            echo json_encode(Code::message('用户未登陆',201101),JSON_UNESCAPED_UNICODE);exit;//没有传用户的Header
        }

//        if(request()->header('timestamp') == '' && request()->header('Timestamp') == ''){
//            echo json_encode(Code::message('登陆状态失效',201101),JSON_UNESCAPED_UNICODE);exit;//没有传用户的Header
//        }
        $request->user_id = request()->header('uid');
        $query = UsersDao::builder_user_query(['user_id'=>$request->user_id]);
        if(!$query->exists()){
            echo json_encode(Code::message('用户不存在',201101),JSON_UNESCAPED_UNICODE);exit;//没有传用户的Header
        }
//        $userInfo = $query->first()->toArray();

//        $timp = request()->header('timestamp');
//        if($timp != Cache::getCacheValue('APP_VERSION_DATA'.$userInfo['username'])){
//            echo json_encode(Code::message('登陆状态失效1',201101),JSON_UNESCAPED_UNICODE);exit;//没有传用户的Header
//        }
        return $next($request);
    }

}
